object brainfuck extends App {
    val input = scala.io.Source.fromInputStream(System.in).getLines.mkString("")
    val jumps = Array.ofDim[Int](input.size)
    
    @scala.annotation.tailrec
    def assignJumps(pos: Int, stack: List[Int]) {
        if (pos < input.size) {
            if (input(pos) == '[') {
                assignJumps(pos + 1, pos :: stack)
            } else if (input(pos) == ']') {
                val h :: t = stack
                jumps(h) = pos
                jumps(pos) = h
                assignJumps(pos + 1, t)
            } else {
                assignJumps(pos + 1, stack)
            }
        }
    }
    assignJumps(0, Nil)
    
    val tape = Array.ofDim[Byte](30000)
    var iptr = 0
    var tptr = 0

    while (input(iptr) != '!') {
        input(iptr) match {
            case '>' => tptr = (tptr + 1) % tape.length
            case '<' => tptr = (tptr - 1 + tape.length) % tape.length
            case '+' => tape(tptr) = (tape(tptr) + 1).toByte
            case '-' => tape(tptr) = (tape(tptr) - 1).toByte
            case '.' => print(s"${tape(tptr)} ")
            case '[' => iptr = if (tape(tptr) == 0) jumps(iptr) else iptr
            case ']' => iptr = jumps(iptr) - 1
            case  a  => 
        }
        iptr += 1
    }
}
